home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gigarom 1
/
Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso
/
FILES
/
DEV
/
A-B
/
ASCII.cpt
/
ASCII.C
next >
Wrap
C/C++ Source or Header
|
1985-10-19
|
12KB
|
497 lines
#include <mac/quickdraw.h>
#include <mac/toolintf.h>
#include <mac/osintf.h>
#include <mac/packintf.h>
#include "ascii.h"
main () {
/* Standard initialisation */
struct QDVar QDVar;
QD = &QDVar;
InitGraf (&thePort);
InitFonts ();
FlushEvents (everyEvent, 0);
InitWindows ();
InitMenus ();
TEInit ();
InitDialogs ( (ProcPtr) 0);
/* InitCursor (); Done in the LOOP */
/* Application dependent initialisation */
init_the_w ();
init_the_text ();
init_the_menu ();
init_the_scrap ();
init_the_cursor ();
init_the_resource (); /* This must be the last one ... */
/* The Main Event LOOP */
while (1){
TEIdle (h_text);
SystemTask ();
GetNextEvent (everyEvent, &my_event);
if (the_w == FrontWindow ()){
adjust_cursor ();
mouse_p.h = my_event.where.h;
mouse_p.v = my_event.where.v;
GlobalToLocal (&mouse_p);
}
switch (my_event.what){
case mouseDown:
switch (FindWindow (&my_event.where, &which_w)){
case inMenuBar:
do_command (MenuSelect (&my_event.where));
break;
case inContent:
if (the_w == FrontWindow ())
the_w_click ();
else
SelectWindow (which_w);
break;
case inDrag:
break; /* No drag done */
case inGoAway:
break; /* Ne exit availible */
case inSysWindow:
SystemClick (&my_event, which_w);
break;
}
break;
case keyDown:
if (the_w != FrontWindow ())
break;
do_key ();
break;
case activateEvt:
if (the_w == FrontWindow ()){
TEActivate (h_text);
DisableItem (my_menus[2], TEUNDO);
EnableItem (my_menus[3], 0);
EnableItem (my_menus[4], 0);
}
else{
TEDeactivate (h_text);
EnableItem (my_menus[2], TEUNDO);
DisableItem (my_menus[3], 0);
DisableItem (my_menus[4], 0);
}
DrawMenuBar ();
break;
case updateEvt:
BeginUpdate (the_w);
SetCursor (*watch_c);
draw_the_w ();
TEUpdate (&text_r, h_text);
ShowCursor ();
EndUpdate (the_w);
break;
}
}
}
init_the_w (){
int x, y;
the_w = GetNewWindow (WINDOW, &w_record, (WindowPtr) - 1);
SetRect (&ascii_r, 18, 18, 418, 320);
SetRect (&text_r, 425, 18, 500, 317);
SetRect (&view_r, 420, 0, 512, 322);
for (x = 0; x < 16; x++)
for (y = 0; y < 16; y++)
SetRect (&char_r[x][y], 18+25*x, 18+19*y, 43+25*x, 37+19*y);
SetPort (the_w);
}
init_the_text (){
char temp_str[256];
h_text = TENew (&text_r, &view_r); /* view_r > text_r */
h_authortext = TENew (&text_r, &view_r);
sample_str = GetString(SAMPLESTR);
author_str = GetString(AUTHORSTR);
c2p_string(*sample_str, temp_str);
TESetText (temp_str, (int) (*sample_str)[0], h_text);
c2p_string(*author_str, temp_str);
TESetText (temp_str, (int) (*author_str)[0], h_authortext);
}
init_the_resource (){
file_num = root_system = HomeResFile ( GetIndResource ("FONT", 1) );
my_own_file = CurResFile ();
}
change_font (size) int size;{ /* 0 means new font => chose smallest */
int font, i;
for (i = 1; i <= num_of_sizes; i++)
CheckItem (my_menus[4], i, 0);
GetItem (my_menus[3], which_font, font_name);
GetFNum (font_name, &font);
if (! size){
for (i = 1; i <= num_of_sizes; i++)
if (RealFont (font, font_sizes[i])){
size = font_sizes[i];
break;
}
if (size > 18)
size = 12;
}
if (! size)
size = 12;
(*h_text)->fontAscent = (size * 3) / 4; /* How do this right ? */
(*h_text)->lineHeight = (*h_text)->txSize = size;
(*h_text)->txFont = font;
TECalText (h_text);
for (i = 1; i <= num_of_sizes; i++)
if (the_w->txSize == font_sizes[i])
CheckItem (my_menus[4], i, 1);
}
init_the_menu (){
int i, dummy;
my_menus[0] = GetMenu(APPLE);
(**my_menus[0]).menuData.s[1] = '\024'; /* Silly thing this !! */
AddResMenu (my_menus[0], "DRVR");
InsertMenu (my_menus[0], 0);
my_menus[1] = GetMenu (FILE);
InsertMenu (my_menus[1], 0);
my_menus[2] = GetMenu (EDIT);
InsertMenu (my_menus[2], 0);
add_font_menu ();
DrawMenuBar ();
}
add_font_menu (){
my_menus[3] = NewMenu (FONTS, "Font");
AddResMenu (my_menus[3], "FONT");
InsertMenu (my_menus[3], 0);
which_font = 1;
CheckItem (my_menus[3], which_font, 1);
change_font (12);
add_size_menu ();
change_font (0);
}
add_size_menu (){
char name[64];
int i;
my_menus[4] = NewMenu (FONTSIZE, font_name);
num_of_sizes = 0;
for (i = 1; i < 128; i++)
if (RealFont (the_w->txFont, i)){
NumToString (i, name);
append_string ("<O", name);
AppendMenu (my_menus[4], name);
font_sizes[++num_of_sizes] = i;
}
else
if (i == 9 || i == 10 || i == 12 || i == 14 || i == 18){
NumToString (i, name);
AppendMenu (my_menus[4], name);
font_sizes[++num_of_sizes] = i;
}
InsertMenu (my_menus[4], 0);
for (i = 1; i <= num_of_sizes; i++)
if (the_w->txSize == font_sizes[i])
CheckItem (my_menus[4], i, 1);
}
append_string (app, str) char *app, *str;{
int i, j;
i = 0;
while (str[i++] != 0 && i < 60);
i--;
j = 0;
while (app[j] != 0 && i + j < 60)
str[i++] = app[j++];
str[i] = 0;
}
init_the_cursor (){
finger_c = GetCursor (FINGER); HNoPurge (finger_c);
watch_c = GetCursor (WATCH); HNoPurge (watch_c);
bar_c = GetCursor (BAR); HNoPurge (bar_c);
}
init_the_scrap (){
ScrapStuff *scrap_stuff;
scrap_stuff = InfoScrap ();
scrap_count = ! ( scrap_stuff->scrapCount );
/* get_scrap (); Dosn't work yet */
}
do_key (){
char the_char;
the_char = my_event.message & 0xff;
InvertRect (&char_r[(the_char>>4) & 0xf][the_char & 0xf]);
TEKey (the_char, h_text);
InvertRect (&char_r[(the_char>>4) & 0xf][the_char & 0xf]);
}
draw_the_w (){
int x, y, save_font, save_size;
EraseRect (&the_w->portRect);
FrameRect (&ascii_r);
for (x = 0; x < 16; x++){
save_font = the_w->txFont;
save_size = the_w->txSize;
the_w->txFont = systemFont;
the_w->txSize = 12;
MoveTo (5, char_r[0][x].top + 14);
if (x < 10)
DrawChar ( (char) ( x + '0') );
else
DrawChar ( (char) ( x + 'a' - 10) );
MoveTo (char_r[x][0].topLeft.h + 5, 10);
if (x < 10)
DrawChar ( (char) ( x + '0') );
else
DrawChar ( (char) ( x + 'a' - 10) );
the_w->txFont = save_font;
the_w->txSize = save_size;
for (y = 0; y < 16; y++){
MoveTo (char_r[x][y].topLeft.h + 5, char_r[x][y].top + 14);
DrawChar ( (char) ( x*16 + y ) );
}
}
}
do_command (help) int help;{
char name[64];
int i;
the_menu = HiWord (help);
the_item = LoWord (help);
switch (the_menu){
case APPLE:
if(the_item == ABOUT){
about();
break;
}
if(the_item == ABOUT + 1)
break;
GetItem (my_menus[0], the_item, name);
{
GrafPtr save_port;
char *da_name ();
GetPort (&save_port);
if (GetNamedResource ("DRVR", name))/* Without leading zero */
OpenDeskAcc (name);
else /* With leading zero */
OpenDeskAcc (da_name (name));
SetPort (save_port);
}
break;
case FILE:
switch (the_item){
case OPEN:
open_file ();
break;
case QUIT:
LoadScrap ();
ExitToShell ();
break;
}
break;
case EDIT:
if (SystemEdit (the_item - 1))
break;
switch (the_item){
case TECUT:
TECut (h_text);
put_scrap ();
break;
case TECOPY:
TECopy (h_text);
put_scrap ();
break;
case TEPASTE:
/* get_scrap (); Dosn't work yet */
TEPaste (h_text);
break;
case TECLEAR:
TEDelete (h_text);
break;
}
break;
case FONTS:
CheckItem (my_menus[3], which_font, 0);
which_font = the_item;
change_font (0);
CheckItem (my_menus[3], which_font, 1);
num_of_sizes = 0;
DeleteMenu (FONTSIZE);
add_size_menu ();
DrawMenuBar ();
InvalRect (&the_w->portRect);
break;
case FONTSIZE:
change_font (font_sizes[the_item]);
InvalRect (&the_w->portRect);
break;
}
HiliteMenu (0);
}
adjust_cursor (){
if (PtInRect (&mouse_p, &ascii_r)){
SetCursor (*finger_c);
return;
}
if (PtInRect (&mouse_p, &text_r)){
SetCursor (*bar_c);
return;
}
InitCursor ();
}
about(){
EraseRect (&text_r);
TEUpdate (&text_r, h_authortext);
while (! Button ());
while (Button ());
EraseRect (&text_r);
TEUpdate (&text_r, h_text);
FlushEvents (everyEvent, 0);
}
the_w_click (){
int x, y;
if (PtInRect (&mouse_p, &ascii_r)){
x = (mouse_p.h - 18) / 25;
y = (mouse_p.v - 18) / 19;
InvertRect (&char_r[x][y]);
TEKey ( (char) ( x*16 + y ) , h_text);
InvertRect (&char_r[x][y]);
}
if (PtInRect (&mouse_p, &text_r))
TEClick (&mouse_p, 0, h_text);
}
put_scrap (){
Handle text_scrap;
ScrapStuff *scrap_stuff;
ZeroScrap ();
text_scrap = (Ptr) TEScrpHandle;
text_scrap = (Ptr) *text_scrap;
HLock (text_scrap);
PutScrap (GetHandleSize (text_scrap), "TEXT", *text_scrap);
HUnlock (text_scrap);
scrap_stuff = InfoScrap ();
scrap_count = scrap_stuff->scrapCount;
}
/* Dosn't work yet --------------------------------------------------------
get_scrap (){
Handle hdl, text_scrap;
int text_length, off, *text_length_ptr;
ScrapStuff *scrap_stuff;
scrap_stuff = InfoScrap ();
if (scrap_count == scrap_stuff->scrapCount)
return;
scrap_count = scrap_stuff->scrapCount;
hdl = NewHandle (0);
text_length = GetScrap (hdl, "TEXT", &off);
if (text_length > 0){
text_scrap = (Ptr) TEScrpHandle;
text_scrap = (Ptr) *text_scrap;
SetHandleSize (text_scrap, text_length);
text_length_ptr = (Ptr) TEScrpLength;
*text_length_ptr = text_length;
HLock (hdl);
HLock (text_scrap);
BlockMove (*hdl, *text_scrap, text_length);
HUnlock (hdl);
HUnlock (text_scrap);
}
DisposHandle (hdl);
}
---------------------------------------------------------------------------*/
open_file (){
Point top_left;
SetPt (&top_left, 50, 50);
SFGetFile (&top_left, "", (ProcPtr) 0, -1, "", (ProcPtr) 0, &file_record);
if (file_record.good){
if (file_num != root_system && file_num != my_own_file){
SetVol (0, volume_num);
CloseResFile (file_num);
}
volume_num = file_record.vRefNum;
SetVol (0, volume_num);
p2cstr (file_record.fName);
if (! EqualString ("System", file_record.fName, 0, 0) ){
file_num = OpenResFile (file_record.fName);
DeleteMenu (FONTS);
DeleteMenu (FONTSIZE);
add_font_menu ();
DrawMenuBar ();
InvalRect (&the_w->portRect);
}
else{
EraseRect (&ascii_r);
InvalRect (&ascii_r);
MoveTo (30, 30);
DrawString ("Can't open System file");
Delay (60);
}
}
}
/*
* Some desk accessories have weird names that begin with a leading zero.
* Since this fools the automatic C/Pascal string conversion stuff, we
* have this little gem; given a C string, it returns a Pascal string
* with leading zero.
*/
char *da_name (s)
char *s;
{
static char ps[32];
int i = 1;
register char *cp,*dp;
cp = s;
dp = &ps[2];
while ((*dp++ = *cp++))
i++;
ps[0] = i;
return (isapstr (ps));
}
/*
* Copy a Pascal string into a buffer, converting it into a C string.
*/
c2p_string(pSource, cDestination)
char *cDestination;
char *pSource;
{
int i;
for (i = 0; i < pSource[0]; ++i)
cDestination[i] = pSource[i + 1];
cDestination[i] = '\0';
}